package com.zhang.e_topic;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.junit.Test;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class A_producer {

    @Test
    public void test1() throws IOException, TimeoutException {
        //1.创建Connection
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.200.114");
        factory.setPort(5672);
        factory.setVirtualHost("/");
        factory.setUsername("guest");
        factory.setPassword("guest");
        Connection connection = factory.newConnection();

        //2.创建channel
        Channel channel = connection.createChannel();

        //3.创建交换机(这里使用的是默认交换机 AMQP_Default)
        /**
         * 参数一 exchange: 交换机名
         * 参数二 type:交换机类型
         * 参数三 durable:是否持久化(保存到磁盘,重启可用)
         * 参数四 autoDelete: 是否自动删除(当没有与交换机绑定的队列时就自动删除)
         * 参数五 internal: 内部使用(一般false)
         * 参数六 arguments: 参数
         *
         */
        channel.exchangeDeclare("e1", BuiltinExchangeType.TOPIC,false,false,false,null);

        //4.创建队列
        channel.queueDeclare("q1",true,false,false,null);
        channel.queueDeclare("q2",true,false,false,null);

        //5.交换机与队列绑定(指定路由key)
        channel.queueBind("q1","e1","#.aaa");
        channel.queueBind("q2","e1","bbb.*");


        //6.发送消息
        for (int i = 1; i <= 5; i++) {
            String message = "hello boy"+i;
            channel.basicPublish("e1","ccc.ddd.aaa",null,message.getBytes());
        }
        //7.释放资源(不用手动释放,因为Connection继承了Closeable,它会自动关闭)
    }
}






